package indi.zhifa.recipe.bailan.busy.controller.api;

import com.querydsl.core.types.Predicate;
import indi.zhifa.recipe.bailan.busy.entity.annotation.JpaQsdl;
import indi.zhifa.recipe.bailan.busy.enums.EOrmEntityStatus;
import indi.zhifa.recipe.bailan.busy.service.IOrmJDBCService;
import indi.zhifa.recipe.bailan.busy.service.IOrmJpaService;
import indi.zhifa.recipe.bailan.busy.service.IOrmMybatisPlusService;
import indi.zhifa.recipe.bailan.busy.service.IOrmMybatisService;
import indi.zhifa.recipe.bailan.busy.entity.po.OrmTestEntity;
import indi.zhifa.recipe.bailan.busy.entity.vo.PageData;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.querydsl.binding.QuerydslPredicate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Api(tags = "ORM测试")
@RequestMapping("/api/ormTest")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class OrmTestApi {

    private final IOrmJDBCService mOrmService;
    private final IOrmMybatisService mOrmMybatisService;
    private final IOrmJpaService mOrmJpaService;

    private final IOrmMybatisPlusService mOrmMybatisPlusService;

    @Operation(summary = "JDBC创建测试数据")
    @GetMapping("/jdbc/init")
    public String jdbcInit(){
        mOrmService.initTestData();
        return "初始化数据成功";
    }

    @Operation(summary = "JDBC分页查询")
    @GetMapping("/jdbc/page")
    public PageData<OrmTestEntity> jdbcPage(
            @Parameter(description = "当前页") @RequestParam(name = "current") int pCurrent,
            @Parameter(description = "页大小") @RequestParam(name = "size") int pSize,
            @Parameter(description = "应用码") @RequestParam(name = "appCode",required = false) String pAppCode,
            @Parameter(description = "模块码") @RequestParam(name = "moduleCode",required = false) String pModuleCode,
            @Parameter(description = "主码") @RequestParam(name = "code",required = false) String pCode,
            @Parameter(description = "名字") @RequestParam(name = "name",required = false) String pName,
            @Parameter(description = "状态") @RequestParam(name = "status",required = false) EOrmEntityStatus pStatus,
            @Parameter(description = "最小值") @RequestParam(name = "valueMin",required = false) Integer pMin,
            @Parameter(description = "最大值") @RequestParam(name = "valueMax",required = false) Integer pMax) {

        PageData<OrmTestEntity> pageData = mOrmService.page(pCurrent,pSize,pAppCode,pModuleCode,pCode,pName,pStatus,pMin,pMax);
        return pageData;
    }

    @Operation(summary = "mybatis创建测试数据")
    @GetMapping("/mybatis/init")
    public String mybatisInit(){
        mOrmMybatisService.initTestData();
        return "初始化数据成功";
    }

    @Operation(summary = "mybatis分页查询")
    @GetMapping("/mybatis/page")
    public PageData<OrmTestEntity> mybatisPage(
            @Parameter(description = "当前页") @RequestParam(name = "current") int pCurrent,
            @Parameter(description = "页大小") @RequestParam(name = "size") int pSize,
            @Parameter(description = "应用码") @RequestParam(name = "appCode",required = false) String pAppCode,
            @Parameter(description = "模块码") @RequestParam(name = "moduleCode",required = false) String pModuleCode,
            @Parameter(description = "主码") @RequestParam(name = "code",required = false) String pCode,
            @Parameter(description = "名字") @RequestParam(name = "name",required = false) String pName,
            @Parameter(description = "状态") @RequestParam(name = "status",required = false) EOrmEntityStatus pStatus,
            @Parameter(description = "最小值") @RequestParam(name = "valueMin",required = false) Integer pMin,
            @Parameter(description = "最大值") @RequestParam(name = "valueMax",required = false) Integer pMax) {

        PageData<OrmTestEntity> pageData = mOrmMybatisService.page(pCurrent,pSize,pAppCode,pModuleCode,pCode,pName,pStatus,pMin,pMax);
        return pageData;
    }

    @Operation(summary = "JPA创建测试数据")
    @GetMapping("/jpa/init")
    public String jpaInit(){
        mOrmJpaService.initTestData();
        return "初始化数据成功";
    }

    @JpaQsdl
    @Operation(summary = "jpa分页查询")
    @GetMapping("/jpa/page")
    public Page<OrmTestEntity> jpaPage(
            @QuerydslPredicate(root = OrmTestEntity.class) Predicate predicate, Pageable page) {
        Page<OrmTestEntity> pageData = mOrmJpaService.page(page,predicate);
        return pageData;
    }

    @Operation(summary = "mybatis-plu创建测试数据")
    @GetMapping("/mp/init")
    public String mpInit(){
        mOrmMybatisPlusService.initTestData();
        return "初始化数据成功";
    }

    @JpaQsdl
    @Operation(summary = "mybatis-plu分页查询")
    @GetMapping("/mp/page")
    public com.baomidou.mybatisplus.extension.plugins.pagination.Page<OrmTestEntity> mpPage(
            @Parameter(description = "当前页") @RequestParam(name = "current") int pCurrent,
            @Parameter(description = "页大小") @RequestParam(name = "size") int pSize,
            @Parameter(description = "应用码") @RequestParam(name = "appCode",required = false) String pAppCode,
            @Parameter(description = "模块码") @RequestParam(name = "moduleCode",required = false) String pModuleCode,
            @Parameter(description = "主码") @RequestParam(name = "code",required = false) String pCode,
            @Parameter(description = "名字") @RequestParam(name = "name",required = false) String pName,
            @Parameter(description = "状态") @RequestParam(name = "status",required = false) EOrmEntityStatus pStatus,
            @Parameter(description = "最小值") @RequestParam(name = "valueMin",required = false) Integer pMin,
            @Parameter(description = "最大值") @RequestParam(name = "valueMax",required = false) Integer pMax) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<OrmTestEntity> pageData = mOrmMybatisPlusService.page(pCurrent,pSize,pAppCode,pModuleCode,pCode,pName,pStatus,pMin,pMax);
        return pageData;
    }

}
